package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * This the container class for the DVD, it stores an Array List of DVDs. It functions 
 * through a singleton pattern, and several methods for CRUD functionality.
 * 
 * @author (Gruppe B) 
 * @version (1.0) 09-11-2013
 */
public class DVDContainer
{
    // instance variables - replace the example below with your own
    private static DVDContainer instance;
    private ArrayList<DVD> DVDs;

    /**
     * Constructor for objects of class DVDContainer
     */
    private DVDContainer()
    {
        DVDs = new ArrayList<DVD>();
    }

    /**
     * This is the singleton pattern, this makes it impossible to instantiate more than one DVD container.
     */
    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }
    
    /**
     * This is the read DVD method, it works by looping through the DVD array list
     * and uses a string parameter to find the desired DVD, then it prints the information out.
     */
    public void readDVD(String title)
    {
        for(DVD dvd : DVDs)
        {
            String DVDTitle = title;
            if(DVDTitle.equals(dvd.getTitle()))
            {
                dvd.printDVDInfo();
            }            
        }
    }
    
    /**
     * The delete DVD method functions by using an iterator with a string search parameter, to find 
     * and delete the found element, also it prints out a message when the element has been deleted. 
     */
    public void deleteDVD(String title)
    {
        Iterator<DVD> it = DVDs.iterator();
        while(it.hasNext()){
            DVD d = it.next();
            String secondTitle = d.getTitle();
            if(secondTitle.equals(title))
            {
                readDVD(title);
                System.out.println("Has been deleted");
                d.getTitle();
                it.remove();
            }
    }
    }
    
    /**
     * The update DVD method functions the same way as the delete method, it takes a search string to find
     * the wanted element. It takes input and changes the respective fields, thus updating the DVD information.
     */
    public void updateDVD(String title, String newTitle, String genre, String releaseDate, String language, String subtitle, double playTime, int copy)
    {
        Iterator<DVD> it = DVDs.iterator();
        while(it.hasNext()){
        DVD d = it.next();
        String secondTitle = d.getTitle();
        if(secondTitle.equals(title))
        {
            d.setTitle(newTitle);
            d.setGenre(genre);
            d.setReleaseDate(releaseDate);
            d.setLanguage(language);
            d.setSubtitle(subtitle);
            d.setPlayTime(playTime);
            d.setCopy(copy);
        }
        }
        
    }
    
    /**
     * This method adds the DVD to the array list
     */
    public void addDVD(DVD dvd)
    {
        DVDs.add(dvd);
    }
    
    /**
     * This method calls the print method in the DVD class.
     */
    public void listDVDs()
    {
        for(DVD dvd : DVDs)
        {
            dvd.printDVDInfo();
        }
    }
    
    /**
     * This method sets the number of copies of the respective DVD, 
     * it takes a search string and finds the wanted element while looping through the array list.
     * Then it changes the amount of copies of the DVD.
     */
    public void setNoOfCopies(String title, int copy)
    {
        Iterator<DVD> it = DVDs.iterator();
        while(it.hasNext()){
        DVD d = it.next();
        String secondTitle = d.getTitle();
        if(secondTitle.equals(title))
        {            
            d.setCopy(copy);
        }
        }
    }

}